<?php

require __DIR__ . '/../bootstrap.php';
require_once __DIR__ . "/../auth.php";
require_once __DIR__ . "/functions.php";
$em = require __DIR__ . '/../example/em.php';

$auth = new \Auth\Auth($em);
$user = $auth->getUser();

//BEGIN KERCH ///////////////////////////////////////////////////////
if (isset($_GET['dateBegin'])) {
    $dateBegin = convertDate($_GET['dateBegin']);
}

if (isset($_GET['dateEnd'])) {
    $dateEnd = convertDate($_GET['dateEnd']);
}

$sql = "SELECT user.name
     , balance.sum
     , balance.old_balance
     , balance.credited
     , balance.current_balance
     , balance.user_rate
     , balance.`date`
FROM
  balance balance
INNER JOIN user
ON balance.user_id = user.id
WHERE
  `date` BETWEEN '$dateBegin 00:00:00' AND '$dateEnd 23:59:59'";


$rows = $em->getConnection()->query($sql)->fetchAll();
/*
echo '<pre>';
print_r($rows);
echo '</pre>';
exit();
*/
$result["aaData"] = array();
$i = 0;
foreach ($rows as $row) {
    $i++;
    $result["aaData"][] = array(
        $i,
        $row['name'],
        $row['sum'],
        $row['old_balance'],
        $row['credited'],
        $row['current_balance'],
        $row['user_rate'].'%',
        $row['date']);
}
echo json_encode($result);

exit();
//END KERCH ///////////////////////////////////////////////////////